﻿using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

/// <summary>数据 安装</summary>
public class CustomDataSetupLoader<T_Data> where T_Data : class
{
	public CustomDataSetupLoader(Action<Dictionary<int, T_Data>, AssetBundleController, CustomDataListLoader> action)
	{
		this.action = action;
	}
	/// <summary>安装</summary>
	private void Setup(Dictionary<int, T_Data> datas, AssetBundleController abc)
	{
		string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(abc.assetBundleName);
		string text = abc.directory + "/list/" + abc.assetBundleName + "_list.txt";
		if (File.Exists(text))
		{
			Console.WriteLine("Load list:" + text);
			CustomDataListLoader customDataListLoader = new CustomDataListLoader();
			customDataListLoader.Mlist_path = text;
			customDataListLoader.Load(text);
			this.action(datas, abc, customDataListLoader);
			return;
		}
		string textPath = fileNameWithoutExtension + "_list";
		TextAsset textAsset = abc.LoadAsset<TextAsset>(textPath);
		if (textAsset != null)
		{
			CustomDataListLoader customDataListLoader2 = new CustomDataListLoader();
			customDataListLoader2.Mlist_path = textPath;
			customDataListLoader2.Load(textAsset);
			this.action(datas, abc, customDataListLoader2);
		}
	}

	/// <summary>安装 查找</summary>
	public void Setup_Search(Dictionary<int, T_Data> datas, string search)
	{
		string text = string.Empty;
		int num = search.LastIndexOf("/");
		if (num != -1)
		{
			text = search.Substring(0, num);
			search = search.Remove(0, num + 1);
		}
		string dirPath = GlobalData.assetBundlePath + "/" + text;
		if (Directory.Exists(dirPath) == false) return;
		string[] files = Directory.GetFiles(dirPath, search, SearchOption.TopDirectoryOnly);
		Array.Sort<string>(files);
		foreach (string path in files)
		{
			if (Path.GetExtension(path).Length != 0) break;
			string text2 = Path.GetFileNameWithoutExtension(path);
			if (text.Length > 0) text2 = text + "/" + text2;

			AssetBundleController assetBundleController = new AssetBundleController();
			assetBundleController.OpenFromFile(GlobalData.assetBundlePath, text2);
			this.Setup(datas, assetBundleController);
			assetBundleController.Close(false);
		}
		if (!Directory.Exists(GlobalData.assetBundlePath + "/list/" + text)) return;

		foreach (string text3 in Directory.GetFiles(GlobalData.assetBundlePath + "/list/" + text, search + "_Mlist.txt"))
		{
			Console.WriteLine("Load Mlist:" + text3);
			StreamReader streamReader = new StreamReader(new FileStream(text3, FileMode.Open));
			string assetBundleName = streamReader.ReadLine();
			string contents = streamReader.ReadToEnd();
			string tempFileName = Path.GetTempFileName();
			File.WriteAllText(tempFileName, contents);
			CustomDataListLoader customDataListLoader = new CustomDataListLoader();
			customDataListLoader.Mlist_path = text3;
			customDataListLoader.Load(tempFileName);
			File.Delete(tempFileName);
			AssetBundleController assetBundleController2 = new AssetBundleController();
			assetBundleController2.OpenFromFile(GlobalData.assetBundlePath, assetBundleName);
			this.action(datas, assetBundleController2, customDataListLoader);
			assetBundleController2.Close(false);
		}
	}

	/// <summary>安装 本地文件</summary>
	public void Setup_Virtual(Dictionary<int, T_Data> datas, string assetBundleName)
	{
		AssetBundleController assetBundleController = new AssetBundleController(true);
		assetBundleController.OpenFromFile(string.Empty, assetBundleName);
		this.Setup(datas, assetBundleController);
		assetBundleController.Close(false);
	}

	/// <summary>安装 Prefab</summary>
	public static void SetupAction_Prefab(Dictionary<int, PrefabData> datas, AssetBundleController abc, CustomDataListLoader loader)
	{
		bool isNew = IsNewCheckFromABC(abc);
		int attributeNo = loader.GetAttributeNo("ID");
		int attributeNo2 = loader.GetAttributeNo("Name");
		int attributeNo3 = loader.GetAttributeNo("Prefab");
		int dataNum = loader.GetDataNum();
		for (int i = 0; i < dataNum; i++)
		{
			int num = int.Parse(loader.GetData(attributeNo, i));
			string data = loader.GetData(attributeNo2, i);
			string data2 = loader.GetData(attributeNo3, i);
			int count = datas.Count;
			PrefabData prefabData = new PrefabData(num, data, abc.assetBundleName, data2, count, isNew);
			prefabData.Mlist_path = loader.Mlist_path;
			if (datas.ContainsKey(prefabData.id))
			{
				GlobalData.Debug_LogWarning(string.Concat(new object[]{"ID重複:",num," ",data}));
				datas[prefabData.id] = prefabData;
			}
			else
			{
				datas.Add(prefabData.id, prefabData);
			}
		}
	}

	/// <summary>安装 Hair</summary>
	public static void SetupAction_Hair(Dictionary<int, HairData> datas, AssetBundleController abc, CustomDataListLoader loader)
	{
		bool isNew = IsNewCheckFromABC(abc);
		int attributeNo = loader.GetAttributeNo("ID");
		int attributeNo2 = loader.GetAttributeNo("Name");
		int attributeNo3 = loader.GetAttributeNo("Prefab");
		int dataNum = loader.GetDataNum();
		for (int i = 0; i < dataNum; i++)
		{
			int id = int.Parse(loader.GetData(attributeNo, i));
			string data = loader.GetData(attributeNo2, i);
			string data2 = loader.GetData(attributeNo3, i);
			int count = datas.Count;
			HairData hairData = new HairData(id, data, abc.assetBundleName, data2, count, isNew);
			hairData.Mlist_path = loader.Mlist_path;
			datas.Add(hairData.id, hairData);
		}
	}

	/// <summary>安装 BackHairData</summary>
	public static void SetupAction_BackHair(Dictionary<int, BackHairData> datas, AssetBundleController abc, CustomDataListLoader loader)
	{
		bool isNew = IsNewCheckFromABC(abc);
		int attributeNo = loader.GetAttributeNo("ID");
		int attributeNo2 = loader.GetAttributeNo("Name");
		int attributeNo3 = loader.GetAttributeNo("Prefab");
		int attributeNo4 = loader.GetAttributeNo("Set");
		int attributeNo5 = loader.GetAttributeNo("Type");
		int dataNum = loader.GetDataNum();
		for (int i = 0; i < dataNum; i++)
		{
			int id = int.Parse(loader.GetData(attributeNo, i));
			string data = loader.GetData(attributeNo2, i);
			string data2 = loader.GetData(attributeNo3, i);
			string data3 = loader.GetData(attributeNo5, i);
			bool isSet = bool.Parse(loader.GetData(attributeNo4, i));
			int count = datas.Count;
			BackHairData backHairData = new BackHairData(id, data, abc.assetBundleName, data2, count, isNew, data3, isSet);
			backHairData.Mlist_path = loader.Mlist_path;
			datas.Add(backHairData.id, backHairData);
		}
	}

	/// <summary>安装 UnderhairData</summary>
	public static void SetupAction_UnderHair(Dictionary<int, UnderhairData> datas, AssetBundleController abc, CustomDataListLoader loader)
	{
		bool isNew = IsNewCheckFromABC(abc);
		int attributeNo = loader.GetAttributeNo("ID");
		int attributeNo2 = loader.GetAttributeNo("Name");
		int attributeNo3 = loader.GetAttributeNo("Prefab");
		int attributeNo4 = loader.GetAttributeNo("Sub");
		int dataNum = loader.GetDataNum();
		for (int i = 0; i < dataNum; i++)
		{
			int id = int.Parse(loader.GetData(attributeNo, i));
			string data = loader.GetData(attributeNo2, i);
			string data2 = loader.GetData(attributeNo3, i);
			int sub = -1;
			string data3 = loader.GetData(attributeNo4, i);
			if (data3 != "-") sub = int.Parse(data3);
			int count = datas.Count;
			UnderhairData underhairData = new UnderhairData(id, data, abc.assetBundleName, data2, sub, count, isNew);
			underhairData.Mlist_path = loader.Mlist_path;
			datas.Add(underhairData.id, underhairData);
		}
	}

	/// <summary>安装 WearData</summary>
	public static void SetupAction_Wear(Dictionary<int, WearData> datas, AssetBundleController abc, CustomDataListLoader loader)
	{
		bool isNew = IsNewCheckFromABC(abc);
		int attributeNo = loader.GetAttributeNo("ID");
		int attributeNo2 = loader.GetAttributeNo("Name");
		int attributeNo3 = loader.GetAttributeNo("Prefab");
		int attributeNo4 = loader.GetAttributeNo("Liquid");
		int attributeNo5 = loader.GetAttributeNo("Normal");
		int attributeNo6 = loader.GetAttributeNo("Coordinates");
		int attributeNo7 = loader.GetAttributeNo("BraDisable");
		int attributeNo8 = loader.GetAttributeNo("ShortsDisable");
		int attributeNo9 = loader.GetAttributeNo("Nip");
		int attributeNo10 = loader.GetAttributeNo("UnderHair");
		int attributeNo11 = loader.GetAttributeNo("Special");
		int dataNum = loader.GetDataNum();
		for (int i = 0; i < dataNum; i++)
		{
			int id = int.Parse(loader.GetData(attributeNo, i));
			string data = loader.GetData(attributeNo2, i);
			string data2 = loader.GetData(attributeNo3, i);
			int order = datas.Count;
			if (data == "なし")
			{
				order = -1;
			}
			WearData wearData = new WearData(id, data, abc.assetBundleName, data2, order, isNew);
			wearData.Mlist_path = loader.Mlist_path;
			string a = "-";
			if (attributeNo4 != -1)
			{
				wearData.liquid = loader.GetData(attributeNo4, i);
			}
			if (attributeNo5 != -1)
			{
				wearData.normal = loader.GetData(attributeNo5, i);
			}
			if (attributeNo6 != -1)
			{
				wearData.coordinates = int.Parse(loader.GetData(attributeNo6, i));
			}
			if (attributeNo7 != -1)
			{
				wearData.braDisable = (int.Parse(loader.GetData(attributeNo7, i)) != 0);
			}
			if (attributeNo8 != -1)
			{
				wearData.shortsDisable = (int.Parse(loader.GetData(attributeNo8, i)) != 0);
			}
			if (attributeNo9 != -1)
			{
				wearData.nip = (int.Parse(loader.GetData(attributeNo9, i)) != 0);
			}
			if (attributeNo10 != -1)
			{
				wearData.underhair = (int.Parse(loader.GetData(attributeNo10, i)) != 0);
			}
			if (attributeNo11 != -1)
			{
				a = loader.GetData(attributeNo11, i);
			}
			if (a == "VR_EVENT")
			{
				wearData.special = ItemDataBase.SPECIAL.VR_EVENT;
			}
			if (!datas.ContainsKey(wearData.id))
			{
				datas.Add(wearData.id, wearData);
			}
			else
			{
				GlobalData.Debug_LogError(string.Concat(new object[] { "ID重複：", wearData.id, " ", wearData.name }));
			}
		}
	}

	/// <summary>安装 AccessoryData</summary>
	public static void SetupAction_Accessory(Dictionary<int, AccessoryData> datas, AssetBundleController abc, CustomDataListLoader loader)
	{
		bool isNew = IsNewCheckFromABC(abc);
		int attributeNo = loader.GetAttributeNo("ID");
		int attributeNo2 = loader.GetAttributeNo("Name");
		int attributeNo3 = loader.GetAttributeNo("Prefab_M");
		int attributeNo4 = loader.GetAttributeNo("Prefab_F");
		int attributeNo5 = loader.GetAttributeNo("Parent");
		int attributeNo6 = loader.GetAttributeNo("Special");
		int dataNum = loader.GetDataNum();
		for (int i = 0; i < dataNum; i++)
		{
			int id = int.Parse(loader.GetData(attributeNo, i));
			string data = loader.GetData(attributeNo2, i);
			string data2 = loader.GetData(attributeNo3, i);
			string data3 = loader.GetData(attributeNo4, i);
			string data4 = loader.GetData(attributeNo5, i);
			int count = datas.Count;
			ItemDataBase.SPECIAL special = ItemDataBase.SPECIAL.NONE;
			string a = "-";
			if (attributeNo6 != -1)
			{
				a = loader.GetData(attributeNo6, i);
			}
			if (a == "VR_EVENT")
			{
				special = ItemDataBase.SPECIAL.VR_EVENT;
			}
			AccessoryData accessoryData = new AccessoryData(id, data, abc.assetBundleName, data2, data3, data4, special, count, isNew);
			accessoryData.Mlist_path = loader.Mlist_path;
			if (!datas.ContainsKey(accessoryData.id))
			{
				datas.Add(accessoryData.id, accessoryData);
			}
			else
			{
				GlobalData.Debug_LogWarning(string.Concat(new object[]{"ID重複：",accessoryData.id," ",accessoryData.name}));
				datas[accessoryData.id] = accessoryData;
			}
		}
	}

	/// <summary>安装 CombineTextureData</summary>
	public static void SetupAction_CombineTexture(Dictionary<int, CombineTextureData> datas, AssetBundleController abc, CustomDataListLoader loader)
	{
		int attributeNo = loader.GetAttributeNo("ID");
		int attributeNo2 = loader.GetAttributeNo("Name");
		int attributeNo3 = loader.GetAttributeNo("Prefab");
		int attributeNo4 = loader.GetAttributeNo("X");
		int attributeNo5 = loader.GetAttributeNo("Y");
		bool isNew = IsNewCheckFromABC(abc);
		int dataNum = loader.GetDataNum();
		for (int i = 0; i < dataNum; i++)
		{
			int count = datas.Count;
			int id = int.Parse(loader.GetData(attributeNo, i));
			string data = loader.GetData(attributeNo2, i);
			string data2 = loader.GetData(attributeNo3, i);
			int x = (attributeNo4 != -1) ? int.Parse(loader.GetData(attributeNo4, i)) : 0;
			int y = (attributeNo5 != -1) ? int.Parse(loader.GetData(attributeNo5, i)) : 0;
			CombineTextureData combineTextureData = new CombineTextureData(id, data, abc.assetBundleName, data2, x, y, count, isNew);
			combineTextureData.Mlist_path = loader.Mlist_path;
			datas.Add(combineTextureData.id, combineTextureData);
		}
	}

	/// <summary>是否第一次使用</summary>
	private static bool IsNewCheckFromABC(AssetBundleController abc)
	{
		string assetBundleName = abc.assetBundleName;
		return assetBundleName.IndexOf("hs00") == -1 && assetBundleName.IndexOf("hsad") == -1 && assetBundleName.IndexOf("hs15") == -1 && assetBundleName.IndexOf("hs01-20") == -1 && assetBundleName.IndexOf("00ph") == -1 && assetBundleName.LastIndexOf("_00") == -1 && assetBundleName.IndexOf("ph00") == -1;
	}

	private Action<Dictionary<int, T_Data>, AssetBundleController, CustomDataListLoader> action;
}
